<template>
  <div>
    <el-card>
      <PageTools>

        <template v-slot:before>
          <el-button
            v-if="checkPerm('addProperty')"
            size="small"
            type="primary"
            @click="addFn"
          >添加房产</el-button>
        </template>

        <template v-slot:after>
          <el-select v-model="cateTypeId" placeholder="全部分类">
            <el-option
              v-for="item in cateType"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>

          <el-select v-model="agentTypeId" placeholder="全部经纪">
            <el-option
              v-for="item in agentType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
          <input v-model="title_contains" type="text">
          <el-button @click="findFn">筛选</el-button>
        </template>

      </PageTools>
      <el-table border :data="propertieList">
        <el-table-column label="封面" width="120">
          <template v-slot="{row}">
            <img :src="row.image" alt="" class="tableImg">
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" />
        <el-table-column label="标签" prop="tags" />
        <el-table-column label="地址" prop="address" />
        <el-table-column label="分类" prop="category.title" />
        <el-table-column label="经纪" prop="agent.name" />
        <el-table-column label="价格" prop="price" />
        <el-table-column label="介绍" prop="desc" width="160" />
        <el-table-column label="图片" width="120">
          <template v-slot="{ row }">
            <el-row v-for="item in row.photos" :key="item">
              <img :src="item" alt="" class="tableImg">
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="145">
          <template v-slot="{row}">
            <el-button
              v-if="checkPerm('editProperty')"
              type="primary"
              size="small"
              @click="editFn(row.id)"
            >编辑</el-button>
            <el-button
              v-if="checkPerm('delProperty')"
              type="danger"
              size="small"
              @click="delFn(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分割线 -->
      <el-divider />
      <el-row type="flex" justify="center">
        <!-- 分页器 -->
        <el-pagination
          layout="prev,pager,next"
          :page-size="size"
          :total="total"
          @current-change="currentChange"
        />
      </el-row>
    </el-card>
    <!-- 弹框  -->
    <AddProperties
      ref="AddProperties"
      :show-dialog.sync="visibleDialog"
      @upadateEvent="getPropertieList()"
    />
  </div>
</template>

<script>
import { getAgents } from '@/api/agents'
import { getCategory } from '@/api/category'
import { getPropertieList, getPropertieNum, delPropertie } from '@/api/properties'
import AddProperties from './components/add-properties.vue'

export default {
  components: { AddProperties },
  data() {
    return {
      // 分页参数
      // 前端概念中的页码
      page: 1,
      // 总条数
      total: 0,
      size: 5,
      propertieList: [],
      visibleDialog: false,
      // 下拉框的信息
      cateType: [],
      agentType: [],
      agent: '',
      category: '',
      cateTypeId: null,
      agentTypeId: null,
      title_contains: null
    }
  },
  async created() {
    await this.getPropertieList()
    // 获取分类列表
    const cateType = await getCategory()
    // console.log(cateType, 'cateType')
    this.cateType = cateType.data
    // 获取经纪列表
    const agentType = await getAgents()
    // console.log(agentType, 'cateType')
    this.agentType = agentType.data
    // 获取房产数量
    this.getPropertieNum()
  },
  methods: {
    // 查询函数
    findFn() {
      const params = {
        category: this.cateTypeId,
        agent: this.agentTypeId,
        title_contains: this.title_contains }
      this.getPropertieList(params)
    },
    // 换页函数
    currentChange(newPage) {
      this.page = newPage
      // 当前下标等于上一页最后下标的后一个，把页码减一，乘以每页的条数

      this.getPropertieList()
    },
    // 获取房产数量
    async getPropertieNum() {
      const res = await getPropertieNum()
      this.total = res.data
    },
    // 获取房产列表
    async getPropertieList(params) {
      const res = await getPropertieList({
        ...params,
        _limit: this.size,
        _start: (this.page - 1) * this.size
      })
      this.propertieList = res.data
    },
    // 新增房产添加按钮
    addFn() {
      this.visibleDialog = true
    },
    // 删除按钮
    async delFn(id) {
      this.$confirm('是否删除', '删除', {
        type: 'warning'
      }).then(async() => {
        await delPropertie(id)
        this.getPropertieList()
      })
    },
    // 编辑按钮
    async editFn(id) {
      this.visibleDialog = true
      this.$refs.AddProperties.getEditor(id)
    }

  }
}
</script>

<style>
.tableImg {
  width: 100px;
  height: 100px;
  object-fit:contain;
}
</style>
